<?php

//CURL发送JSON
function curlJson($url, $data, $token='', $method="POST") {

    $data_string = json_encode($data);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

    if(!isNullOrEmpty($token)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' .strlen($data_string),
            'Authorization: Bearer ' .$token
        ));
    } else {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' .strlen($data_string))
        );
    }

    if(0 === strpos(strtolower($url), 'https')) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //从证书中检查SSL加密算法是否存在
    }

    $result = curl_exec($ch);

    return $result;
}

//获取环信Token
function getEaseMobValidToken() {

    $map['is_valid'] = C('DEFAULT_YN.YES');

    $chatToken = D('chatToken')
        ->where($map)
        ->order('create_time DESC')
        ->find();

    while(isNullOrEmpty($chatToken)) {
        $chatToken = D('chatToken')
            ->where($map)
            ->order('create_time DESC')
            ->find();

        usleep(1000000);
    }

    return $chatToken['token'];

}

//环信用户注册接口
function easeMobRegisterApi($username, $password) {

    $url = "https://a1.easemob.com/".C('EASEMOB_ORG_NAME')."/".C("EASEMOB_APP_NAME")."/users";

    $data = array(
        'username' => $username,
        'password' => $password,
    );

    $result = curlJson($url,$data,getEaseMobValidToken());

    $result = json_decode($result,true);

    return $result;

}

//注册环信用户
function registerEaseMob($username,$password) {

    //注册环信
    $result = easeMobRegisterApi($username,$password);

    $i = 0;
    while (!isNullOrEmpty($result['error'])) {
        $i++;
        $result = easeMobRegisterApi($username,$password);

        if($i == 10) {
            return false;
        }
    }

    //判断用户注册是否正确
    if($result['entities'][0]['username'] == $username) {
        return true;
    } else {
        return false;
    }

}

//环信创建群组接口
function easeMobGroupCreateApi($groupName, $description) {

    $url = "https://a1.easemob.com/".C('EASEMOB_ORG_NAME')."/".C("EASEMOB_APP_NAME")."/chatgroups";

    $data = array(
        'groupname' => $groupName,
        'desc' => $description,
        'public' => true,
        'approval' => false,
        'owner' => C('EASEMOB_GROUP_OWNER_ID'),
        'maxusers' => C('EASEMOB_GROUP_MAX_MEMBER')
    );

    $token = getEaseMobValidToken();

    $result = curlJson($url,$data,$token);

    $result = json_decode($result,true);

    return $result;

}

//环信修改密码接口
function easeMobChangePasswordApi($username, $newPassword) {

    $url = "https://a1.easemob.com/".C('EASEMOB_ORG_NAME')."/".C("EASEMOB_APP_NAME")."/users/".$username."/password";

    $data = array(
        'newpassword' => $newPassword
    );

    $token = getEaseMobValidToken();

    $result = curlJson($url,$data,$token,"PUT");

    $result = json_decode($result,true);

    if(!isNullOrEmpty($result['exception'])) {
        return false;
    } else {
        return true;
    }

}